package uk.ac.staffs.student.autopilot;

import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;

import uk.ac.staffs.student.autopilot.packet.PacketInterface;
import uk.ac.staffs.student.autopilot.packet.RtfPacket;
import uk.ac.staffs.student.autopilot.views.ApHciSwingUI;

public class AutoPilotApp implements AutoPilotInterface {

    public static Logger logger = Logger.getLogger(AutoPilotApp.class);
    protected SerialPortManager portManager;
    
    public AutoPilotApp(SerialPortManager portManager){
        this.portManager = portManager;
    }
    /* (non-Javadoc)
     * @see uk.ac.staffs.student.autopilot.AutoPilotInterface#exit()
     */
    @Override
    public void exit(){
        logger.info("Exit AutPilotApp called, closing serial port");
        this.portManager.close();
        System.exit(0);
    }
    /* (non-Javadoc)
     * @see uk.ac.staffs.student.autopilot.AutoPilotInterface#sendPacket(uk.ac.staffs.student.autopilot.packet.PacketInterface)
     */
    @Override
    public boolean sendRtfPacket(RtfPacket packet){
        // Do your portManager comms stuff here.
        // something line this.portManager.serialPort.SomeMethod to send a packet
        logger.info("Sending RTF packet (need to call portManager here to send it...: " + packet);
        return false;
    }
    /**
     * @param args
     */
    public static void main(final String[] args) {

        // Start the serial port manager for packet retrieval.
        // final SerialPortManagerMock portManager = new
        // SerialPortManagerMock();
        final SerialPortManager portManager = new SerialPortManager();
        final AutoPilotApp controller = new AutoPilotApp(portManager);
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final ApHciSwingUI ui = new ApHciSwingUI(controller);
                portManager.getPacketBuilder().addObserver(ui.getViewPanel());
                //ui.AddListener(this);
                ui.setVisible(true);
                
            }
        });

        // Thread portThread = new Thread(portManager);
        // portThread.start();
        //

    }

}
